package com.ossean;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;


@Component
public class ClearTable {
	Logger logger = Logger.getLogger(ClearTable.class);
	public void start(){
		truncateTable(TableName.eddRelationTableName);
		//updateMark(TableName.gatherProjectsTableName,1);
		deleteItemInEddPointers(TableName.pointerTableName,TableName.gatherProjectsTableName, TableName.eddRelationTableName);
		logger.info("完成去重程序运行前数据表的清空");
	}
	//删除edd_pointers表中transfer对应的数据
	public void deleteItemInEddPointers(String pointerTable,String sourceTableName, String targetTableName){
		String sql = "delete from "+pointerTable+" where SourceTableName=? and TargetTableName=?";
		Connection conn = getConnection();
		if(conn == null){
			logger.info("没有获取到Connection");
			System.exit(-1);
		}
		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, sourceTableName);
			ps.setString(2, targetTableName);
			ps.execute();
			ps.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void truncateTable(String table){
		String sql = "truncate table " + table;
		Connection conn = getConnection();
		if(conn == null){
			logger.info("没有获取到Connection");
			System.exit(-1);
		}
		PreparedStatement ps;
		try {
			ps = conn.prepareStatement(sql);
			ps.execute();
			ps.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public void updateMark(String table,int mark){
		String sql = "update " + table+" set update_mark= "+mark;
		Connection conn = getConnection();
		if(conn == null){
			logger.info("没有获取到Connection");
			System.exit(-1);
		}
		PreparedStatement ps;
		try {
			ps = conn.prepareStatement(sql);
			ps.execute();
			ps.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public void updateFlag(String table,Object flag){
		String sql = "update " + table+" set flag= "+flag;
		Connection conn = getConnection();
		if(conn == null){
			logger.info("没有获取到Connection");
			System.exit(-1);
		}
		PreparedStatement ps;
		try {
			ps = conn.prepareStatement(sql);
			ps.execute();
			ps.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public void deleteTaggings4Project(){
		String sql = "delete from taggings where taggable_type=?";
		Connection conn = getConnection();
		if(conn == null){
			logger.info("没有获取到Connection");
			System.exit(-1);
		}
		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, "OpenSourceProject");
			ps.execute();
			ps.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public Connection getConnection(){
		Connection conn = null;
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			conn = DriverManager.getConnection(TableName.connection, TableName.user, TableName.psw);
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
	
	public static void main(String[] args){
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:/applicationContext*.xml");
		ClearTable Main = applicationContext.getBean(ClearTable.class);
		Main.start();
	}
}
